<?php
  include("DBConfig.class.php");
  class Person{

    public function getPerson($value){

      //Query Province
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );


        $value['pro'] = urldecode($value['pro']);
        $value['dis'] = urldecode($value['dis']);
        $value['tam'] = urldecode($value['tam']);
        $value['moo'] = urldecode($value['moo']);
        $value['hou'] = urldecode($value['hou']);

        $sql = "SELECT 
                  province,
                  district,
                  tambon,
                  moo,
                  house_no,
                  house_no_lat,
                  house_no_lon,
                  firstname,
                  lastname,
                  gender,
                  trok,
                  soi,
                  pid,
                  id
                FROM
                  ".DBConfig::$dbfieldname."
                WHERE
                  province = :pro
                AND
                  district = :dis
                AND
                  tambon = :tam
                AND
                  moo = :moo
                AND
                  house_no = :hou
                ORDER BY  lastname,firstname ASC";
        $stmt = $db->prepare($sql);

        $stmt->bindParam( ':pro', $value['pro'], PDO::PARAM_STR );
        $stmt->bindParam( ':dis', $value['dis'], PDO::PARAM_STR );
        $stmt->bindParam( ':tam', $value['tam'], PDO::PARAM_STR );
        $stmt->bindParam( ':moo', $value['moo'], PDO::PARAM_STR );
        $stmt->bindParam( ':hou', $value['hou'], PDO::PARAM_STR );
        
        $stmt->execute();
        $result = $stmt->fetchAll();

        if(!empty($result)){
          $response['votename'] = $value['votename'];
          $response['country'] = $value['country'];
          $response['layer'] = $result;
          $response['errorCode'] = 0;
          $response['errorString'] = "ok";
          $this->setPersonResponse($response);
        } else{
          $response['errorCode'] = 107;
          $response['errorString'] = "Person not found.";
          $this->setPersonResponse($response);
        }

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          //echo $e->getMessage();
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $this->setProvinceResponse($response);
      }
    }

    public function searchPerson($value){

      //Query Province
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  province,
                  district,
                  tambon,
                  moo,
                  house_no,
                  house_no_lat,
                  house_no_lon,
                  firstname,
                  lastname,
                  gender,
                  trok,
                  soi,
                  pid,
                  id
                FROM
                  ".DBConfig::$dbfieldname."
                WHERE
                    firstname like '".$value['first']."%'
                AND
                    lastname like '".$value['last']."%'
                ORDER BY  lastname,firstname ASC";

        $stmt = $db->prepare($sql);

        $stmt->execute();
        $result = $stmt->fetchAll();

        if(!empty($result)){
          $response['votename'] = $value['votename'];
          $response['country'] = $value['country'];
          $response['layer'] = $result;
          $response['errorCode'] = 0;
          $response['errorString'] = "ok";
          $this->setPersonSearchResponse($response);
        } else{
          $response['errorCode'] = 107;
          $response['errorString'] = "Person not found.";
          $this->setPersonSearchResponse($response);
        }

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          //echo $e->getMessage();
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $this->setPersonSearchResponse($response);
      }
    }

    public function searchFirstName($value){

      //Query Province
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  province,
                  district,
                  tambon,
                  moo,
                  house_no,
                  house_no_lat,
                  house_no_lon,
                  firstname,
                  lastname,
                  gender,
                  trok,
                  soi,
                  pid,
                  id
                FROM
                  ".DBConfig::$dbfieldname."
                WHERE
                  firstname like '".$value['first']."%'
                ORDER BY lastname,firstname ASC";
        $stmt = $db->prepare($sql);

        $stmt->execute();
        $result = $stmt->fetchAll();

        if(!empty($result)){
          $response['votename'] = $value['votename'];
          $response['country'] = $value['country'];
          $response['layer'] = $result;
          $response['errorCode'] = 0;
          $response['errorString'] = "ok";
          $this->setPersonSearchResponse($response);
        } else{
          $response['errorCode'] = 107;
          $response['errorString'] = "Person not found.";
          $this->setPersonSearchResponse($response);
        }

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          //echo $e->getMessage();
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $this->setPersonSearchResponse($response);
      }
    }

    public function searchLastName($value){

      //Query Province
      try {
        $db = new PDO("mysql:host=".DBConfig::$dbhost.";dbname=".DBConfig::$dbname, 
                      DBConfig::$dbuser, 
                      DBConfig::$dbpass, 
                      array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
                  );
        $db->setAttribute( PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION );

        $sql = "SELECT 
                  province,
                  district,
                  tambon,
                  moo,
                  house_no,
                  house_no_lat,
                  house_no_lon,
                  firstname,
                  lastname,
                  gender,
                  trok,
                  soi,
                  pid,
                  id
                FROM
                  ".DBConfig::$dbfieldname."
                WHERE
                  lastname like '".$value['last']."%'
                ORDER BY lastname,firstname ASC";
        $stmt = $db->prepare($sql);

        $stmt->execute();
        $result = $stmt->fetchAll();

        if(!empty($result)){
          $response['votename'] = $value['votename'];
          $response['country'] = $value['country'];
          $response['layer'] = $result;
          $response['errorCode'] = 0;
          $response['errorString'] = "ok";
          $this->setPersonSearchResponse($response);
        } else{
          $response['errorCode'] = 107;
          $response['errorString'] = "Person not found.";
          $this->setPersonSearchResponse($response);
        }

        /*** close the database connection ***/
        $db = null;
      }catch(PDOException $e) {
          //echo $e->getMessage();
          $response['errorCode'] = 5;
          $response['errorString'] = "Database cann't connect";
          $this->setPersonSearchResponse($response);
      }
    }

    public function setPersonResponse($response){

        //Gen food
        $xml = new DOMDocument('1.0', 'UTF-8');
        $xml->formatOutput = true;

        $xml_node_datas = $xml->createElement("Datas");
        $xml->appendChild( $xml_node_datas);


        if($response['errorCode'] == 0){
          $xml_node_name = $xml->createElement("name",$response['votename']);
          $xml_node_datas->appendChild( $xml_node_name);

          $xml_node_country = $xml->createElement("country",$response['country']);
          $xml_node_datas->appendChild( $xml_node_country);

          $xml_node_data = $xml->createElement("Data");
          $xml_node_data->setAttribute("id", 1);
          $xml_node_datas->appendChild( $xml_node_data);

          $xml_node_type = $xml->createElement("type","map");
          $xml_node_data->appendChild( $xml_node_type);

          //Check thai country 
          if($response['country'] == "TH"){
            $title = "สถานที่ตั้งบ้านเลขที่ ".$response['layer'][0]['house_no'];
          }else{
            $title = $response['layer'][0]['house_no']." location ";
          }

          $xml_node_title = $xml->createElement("title",$title);
          $xml_node_data->appendChild( $xml_node_title);

          $xml_node_address = $xml->createElement("address",$response['layer'][0]['house_no']);
          $xml_node_data->appendChild( $xml_node_address);

          $xml_node_lat = $xml->createElement("latitude",$response['layer'][0]['house_no_lat']);
          $xml_node_data->appendChild( $xml_node_lat);

          $xml_node_lon = $xml->createElement("longitude",$response['layer'][0]['house_no_lon']);
          $xml_node_data->appendChild( $xml_node_lon);
          $count = 2;
          foreach($response['layer'] as $val){

            $xml_node_data = $xml->createElement("Data");
            $xml_node_data->setAttribute("id", $count++);
            $xml_node_datas->appendChild( $xml_node_data);


            $xml_node_type = $xml->createElement("type","person");
            $xml_node_data->appendChild( $xml_node_type);

            $title = $val['firstname']." ".$val['lastname'];

            $address = "";
            if($response['country'] == "TH"){
              if($val['house_no']){
                $address .= $val['house_no']." ";
              }
              if($val['soi']){
                $address .= " ซ.".$val['soi'];
              }
              if($val['trok']){
                $address .= " ตรอก ".$val['trok'];
              }
              if($val['moo']){
                $address .= " ม.".$val['moo'];
              }

              if($val['tambon']){
                $tambon = "ต.".$val['tambon'];
              }
              if($val['district']){
                $district = "อ.".$val['district'];
              }
              if($val['province']){
                $province = "จ.".$val['province'];
              }
            }else{
              if($val['house_no']){
                $address .= $val['house_no'];
              }
              if($val['soi']){
                $address .= ", ".$val['soi'];
              }
              if($val['trok']){
                $address .= ", ".$val['trok'];
              }
              if($val['moo']){
                $address .= ", ".$val['moo'];
              }

              if($val['tambon']){
                $tambon = $val['tambon'];
              }
              if($val['district']){
                $district = $val['district'];
              }
              if($val['province']){
                $province = $val['province'];
              }
            }

            $xml_node_pid = $xml->createElement("pid",  $val['pid']);
            $xml_node_data->appendChild( $xml_node_pid);
            $xml_node_title = $xml->createElement("title", $title);
            $xml_node_data->appendChild( $xml_node_title);
            $xml_node_gender = $xml->createElement("gender", $val['gender']);
            $xml_node_data->appendChild( $xml_node_gender);
            $xml_node_address = $xml->createElement("address", $address);
            $xml_node_data->appendChild( $xml_node_address);
            $xml_node_tambon = $xml->createElement("tambon", $val['tambon']);
            $xml_node_data->appendChild( $xml_node_tambon);
            $xml_node_district = $xml->createElement("district", $val['district']);
            $xml_node_data->appendChild( $xml_node_district);
            $xml_node_province = $xml->createElement("province", $val['province']);
            $xml_node_data->appendChild( $xml_node_province);
          }

          $xml_node_errorcode = $xml->createElement( "errorcode", $response['errorCode']);
          $xml_node_datas->appendChild( $xml_node_errorcode );
          $xml_node_errorstring = $xml->createElement( "errorstring", $response['errorString']);
          $xml_node_datas->appendChild( $xml_node_errorstring );

        }else{
          $xml_node_errorcode = $xml->createElement( "errorcode", $response['errorCode']);
          $xml_node_datas->appendChild( $xml_node_errorcode );
          $xml_node_errorstring = $xml->createElement( "errorstring", $response['errorString']);
          $xml_node_datas->appendChild( $xml_node_errorstring );
        }

        echo $xml->saveXML();
    }


    public function setPersonSearchResponse($response){

        //Gen food
        $xml = new DOMDocument('1.0', 'UTF-8');
        $xml->formatOutput = true;

        $xml_node_datas = $xml->createElement("Datas");
        $xml->appendChild( $xml_node_datas);


        if($response['errorCode'] == 0){
          $xml_node_name = $xml->createElement("name",$response['votename']);
          $xml_node_datas->appendChild( $xml_node_name);

          $xml_node_country = $xml->createElement("country",$response['country']);
          $xml_node_datas->appendChild( $xml_node_country);

          $count = 1;
          foreach($response['layer'] as $val){

            $xml_node_data = $xml->createElement("Data");
            $xml_node_data->setAttribute("id", $count++);
            $xml_node_datas->appendChild( $xml_node_data);


            $xml_node_type = $xml->createElement("type","person");
            $xml_node_data->appendChild( $xml_node_type);

            $title = $val['firstname']." ".$val['lastname'];

            $address = "";
            if($response['country'] == "TH"){
              if($val['house_no']){
                $address .= $val['house_no']." ";
              }
              if($val['soi']){
                $address .= " ซ.".$val['soi'];
              }
              if($val['trok']){
                $address .= " ตรอก ".$val['trok'];
              }
              if($val['moo']){
                $address .= " ม.".$val['moo'];
              }

              if($val['tambon']){
                $tambon = "ต.".$val['tambon'];
              }
              if($val['district']){
                $district = "อ.".$val['district'];
              }
              if($val['province']){
                $province = "จ.".$val['province'];
              }
            }else{
              if($val['house_no']){
                $address .= $val['house_no'];
              }
              if($val['soi']){
                $address .= ", ".$val['soi'];
              }
              if($val['trok']){
                $address .= ", ".$val['trok'];
              }
              if($val['moo']){
                $address .= ", ".$val['moo'];
              }

              if($val['tambon']){
                $tambon = $val['tambon'];
              }
              if($val['district']){
                $district = $val['district'];
              }
              if($val['province']){
                $province = $val['province'];
              }
            }

            $xml_node_pid = $xml->createElement("pid",  $val['pid']);
            $xml_node_data->appendChild( $xml_node_pid);
            $xml_node_title = $xml->createElement("title", $title);
            $xml_node_data->appendChild( $xml_node_title);
            $xml_node_gender = $xml->createElement("gender", $val['gender']);
            $xml_node_data->appendChild( $xml_node_gender);
            $xml_node_address = $xml->createElement("address", $address);
            $xml_node_data->appendChild( $xml_node_address);
            $xml_node_tambon = $xml->createElement("tambon", $val['tambon']);
            $xml_node_data->appendChild( $xml_node_tambon);
            $xml_node_district = $xml->createElement("district", $val['district']);
            $xml_node_data->appendChild( $xml_node_district);
            $xml_node_province = $xml->createElement("province", $val['province']);
            $xml_node_data->appendChild( $xml_node_province);
          }

          $xml_node_errorcode = $xml->createElement( "errorcode", $response['errorCode']);
          $xml_node_datas->appendChild( $xml_node_errorcode );
          $xml_node_errorstring = $xml->createElement( "errorstring", $response['errorString']);
          $xml_node_datas->appendChild( $xml_node_errorstring );

        }else{
          $xml_node_errorcode = $xml->createElement( "errorcode", $response['errorCode']);
          $xml_node_datas->appendChild( $xml_node_errorcode );
          $xml_node_errorstring = $xml->createElement( "errorstring", $response['errorString']);
          $xml_node_datas->appendChild( $xml_node_errorstring );
        }

        echo $xml->saveXML();
    }

  }


?>